<template>
  <div class="detail">
    <Header/>

    <div class="detail-content">
      <div class="detail-content-top">
        <div style="position: relative">
          <div class="thing-infos-view">
            <div class="thing-infos">
              <div class="thing-img-box">
                <img :src="detailData.cover"/>
              </div>
              <div class="thing-info-box">
                <div class="thing-state">
                  <span class="state hidden-sm">信息状态</span>
                  <span>有效</span>
                </div>
                <h1 class="thing-name">{{ detailData.title }}</h1>
                <span>
                  <span class="a-price-symbol"></span>
                  <span class="a-price" style="font-size: 20px">{{ detailData.price }}元/时</span>
                </span>
                <div class="translators flex-view" style="">
                  <span>服务：</span>
                  <span class="name">{{ detailData.classification_title }}</span>
                </div>
                <div class="translators flex-view" style="">
                  <span>性别：</span>
                  <span class="name">{{ detailData.sex }}</span>
                </div>
                <div class="translators flex-view" style="">
                  <span>年龄：</span>
                  <span class="name">{{ detailData.age }}岁</span>
                </div>
                <div class="translators flex-view" style="">
                  <span>地区：</span>
                  <span class="name">{{ detailData.location }}</span>
                </div>
                <a-popconfirm title="确定预约?" ok-text="是" cancel-text="否" @confirm="handleOrder(detailData)">
                  <button class="buy-btn">
                    <img :src="AddIcon"/>
                    <span>立即预约</span>
                  </button>
                </a-popconfirm>
              </div>
            </div>
            <div class="thing-counts hidden-sm">
              <div class="count-item flex-view pointer" @click="addToWish()">
                <div class="count-img">
                  <img :src="WantIcon"/>
                </div>
                <div class="count-box flex-view">
                  <div class="count-text-box">

                    <span class="count-title">加入心愿单</span>
                  </div>
                  <div class="count-num-box">
                    <span class="num-text">{{ detailData.wish_count }}</span>
                  </div>
                </div>
              </div>
              <div class="count-item flex-view pointer" @click="collect()">
                <div class="count-img">
                  <img :src="RecommendIcon"/>
                </div>
                <div class="count-box flex-view">
                  <div class="count-text-box">
                    <span class="count-title">收藏</span>
                  </div>
                  <div class="count-num-box">
                    <span class="num-text">{{ detailData.collect_count }}</span>
                  </div>
                </div>
              </div>
              <div class="count-item flex-view" @click="share()">
                <div class="count-img">
                  <img :src="ShareIcon"/>
                </div>
                <div class="count-box flex-view">
                  <div class="count-text-box">
                    <span class="count-title">分享</span>
                  </div>
                  <div class="count-num-box">
                    <span class="num-text"></span>
                    <img :src="WeiboShareIcon" class="mg-l"/>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="detail-content-bottom">
        <div class="thing-content-view flex-view">
          <div class="main-content">
            <div class="order-view main-tab">
              <span
                  class="tab"
                  :class="selectTabIndex === index ? 'tab-select' : ''"
                  v-for="(item, index) in tabData"
                  :key="index"
                  @click="selectTab(index)"
              >
                {{ item }}
              </span>
              <span :style="{ left: tabUnderLeft + 'px' }" class="tab-underline"></span>
            </div>

            <!--简介-->
            <div class="thing-intro" :class="selectTabIndex <= 0 ? '' : 'hide'">
              <p class="text" style="">{{ detailData.description }}</p>
            </div>

            <!--评论-->
            <div class="thing-comment" :class="selectTabIndex > 0 ? '' : 'hide'">
              <div class="title">发表新的评论</div>
              <div class="publish flex-view">
                <img :src="user_cover" class="mine-img"/>
                <input placeholder="说点什么..." class="content-input" ref="commentRef"/>
                <button class="send-btn" @click="sendComment()">发送</button>
              </div>
              <div class="tab-view flex-view">
                <div class="count-text">共有{{ commentData.length }}条评论</div>
                <div class="tab-box flex-view" v-if="commentData.length > 0">
                  <span :class="sortIndex === 0 ? 'tab-select' : ''" @click="sortCommentList('recent')">最新</span>
                  <div class="line"></div>
                  <span :class="sortIndex === 1 ? 'tab-select' : ''" @click="sortCommentList('hot')">热门</span>
                </div>
              </div>
              <div class="comments-list">
                <div class="comment-item" v-for="item in commentData">
                  <div class="flex-item flex-view">
                    <img :src="user_cover" class="avator"/>
                    <div class="person">
                      <div class="name">{{ item.username }}</div>
                      <div class="time">{{ item.comment_time }}</div>
                    </div>
                    <div class="float-right">
                      <span @click="like(item.id)">推荐</span>
                      <span class="num">{{ item.like_count }}</span>
                    </div>
                  </div>
                  <p class="comment-content">{{ item.content }}</p>
                </div>
                <div class="infinite-loading-container">
                  <div class="infinite-status-prompt" style="">
                    <div slot="no-results" class="no-results">
                      <div></div>
                      <p>没有更多了</p>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="recommend" style="">
            <div class="title">热门推荐</div>
            <div v-for="item in recommendData" :key="item.id">
              <div><img :src="item.cover" width="240" height="180" alt="热门图片"></div>
              <div>
                <span style="color:#0f1111;font-size: 16px">&nbsp;&nbsp;&nbsp;{{ item.description.substring(0, 4) }}</span>
              </div>
              <div>
                <span class="a-price">&nbsp;&nbsp;&nbsp;{{ item.price }}元/时 {{item.location}}</span>
              </div>

            </div>
          </div>
        </div>
      </div>
    </div>

    <Footer/>
  </div>
</template>
<script setup>
import {message} from 'ant-design-vue';
import Header from '/@/views/index/components/header.vue';
import Footer from '/@/views/index/components/footer.vue';
import AddIcon from '/@/assets/images/add.svg';
import WantIcon from '/@/assets/images/want-read-hover.svg';
import RecommendIcon from '/@/assets/images/recommend-hover.svg';
import ShareIcon from '/@/assets/images/share-icon.svg';
import WeiboShareIcon from '/@/assets/images/wb-share.svg';
import AvatarIcon from '/@/assets/images/avatar.jpg';

import {addCollectUserApi, addWishUserApi, detailApi,URL, listApi as listThingList} from '/@/api/index/thing';
import {createApi as createCommentApi, likeApi, listThingCommentsApi} from '/@/api/index/comment';
import {BASE_URL, MEDIA_URL} from '/@/store/constants';
// import { MEDIA_URL } from '/@/store/constants';
import {useRoute, useRouter} from 'vue-router/dist/vue-router';
import {useUserStore} from '/@/store';
import {createOrderApi} from '/@/api/index/order';
import axios from 'axios';

const router = useRouter();
const route = useRoute();
const userStore = useUserStore();

let thingId = ref('');
let detailData = ref({});
let tabUnderLeft = ref(6);
let tabData = ref(['简介', '评论']);
let selectTabIndex = ref(0);

let commentData = ref([]);
let recommendData = ref([]);
let sortIndex = ref(0);
let order = ref('recent'); // 默认排序最新
const user_cover = ref();
let commentRef = ref();
onMounted(() => {
  const storedCover = localStorage.getItem('user_cover');
  console.log('storedCover',storedCover)
  if (storedCover) {
    user_cover.value = storedCover;
  }
});

onMounted(() => {
  thingId.value = route.query.id.trim();
  getThingDetail1();
  getRecommendThing();
  getCommentList();
  // addToWish();
  // collect();
  // getThingDetail2();
});

const selectTab = (index) => {
  selectTabIndex.value = index;
  tabUnderLeft.value = 6 + 54 * index;
};

const getThingDetail1 = () => {
  detailApi({id: thingId.value})
      .then((res) => {
        detailData.value = res.data;
        detailData.value.cover = MEDIA_URL + detailData.value.cover;
        // window.location.reload();
      })
      .catch((err) => {
        message.error('获取详情失败');
      });
};
const getThingDetail2 = () => {
  detailApi({id: thingId.value})
      .then((res) => {

        detailData.value = res.data;
        detailData.value.cover = MEDIA_URL + detailData.value.cover;

      })
      .catch((err) => {
        message.error('获取详情失败');
      });
};
const addToWish = () => {
  let username = userStore.user_name;
  if (username) {
    console.log('detailData.wish_count',detailData.value.wish_count)
    addWishUserApi({thingId: thingId.value, username: username})
        .then((res) => {
          setTimeout(() => {
            message.success(res.msg);
            getThingDetail1();
          }, );
        });
  } else {
    message.warn('请先登录');
  }
};
const collect = () => {
  let username = userStore.user_name;
  if (username) {
    addCollectUserApi({thingId: thingId.value, username: username})
        .then((res) => {
          setTimeout(() => {
            message.success(res.msg);
            getThingDetail1();
          }, );
        })
  } else {
    message.warn('请先登录');
  }
};
const share = () => {
  let content = '分享一个非常好玩的网站 ' + window.location.href;
  let shareHref = 'http://service.weibo.com/share/share.php?title=' + content;
  window.open(shareHref);
};

const handleOrder = (detailData) => {
  console.log(detailData);
  const userId = userStore.user_id;
  if (userId) {
          router.push(`confirm/?id=${thingId.value}`);
        }
  else {
    message.warn('请先登录！');
  }
}
const getRecommendThing = () => {
  listThingList({sort: 'recommend'})
      .then((res) => {
        res.data.forEach((item, index) => {
          if (item.cover) {
            item.cover = MEDIA_URL + item.cover;
          }
        });
        recommendData.value = res.data.slice(0, 6);
        console.log(recommendData.value);
      })
      .catch((err) => {
        console.log(err);
      });
};
const handleDetail = (item) => {
  // 跳转新页面
  let text = router.resolve({name: 'detail', query: {id: item.id}});
  window.open(text.href, '_blank');
};
const sendComment = () => {
  // console.log('commentRef.value', commentRef.value);
  let text = commentRef.value.value.trim();
  // console.log('text, text');
  if (text.length <= 0) {
    return;
  }
  commentRef.value.value = '';
  let userId = userStore.user_id;
  if (userId) {
    createCommentApi({content: text, thing: thingId.value, user: userId})
        .then((res) => {
          getCommentList();
        })
        .catch((err) => {
          console.log(err);
        });
  } else {
    message.warn('请先登录！');
    router.push({name: 'login'});
  }
};
const getCommentList = () => {
  listThingCommentsApi({thingId: thingId.value, order: order.value})
      .then((res) => {
        commentData.value = res.data;
        commentData.value.like_count=res.data.like_count
      })
      .catch((err) => {
        console.log(err);
      });
};
const like = (commentId) => {
  let username = userStore.user_name;
  if (username) {
    likeApi({commentId: commentId})
        .then((res) => {
          setTimeout(() => {
            message.success(res.msg);
            getCommentList()

          }, );
        })
        .catch((err) => {
          console.log(res.msg);
        });
  } else {
    message.warn('请先登录');
  }

};

const sortCommentList = (sortType) => {
  if (sortType === 'recent') {
    sortIndex.value = 0;
  } else {
    sortIndex.value = 1;
  }
  order.value = sortType;
  getCommentList();
};
</script>
<style scoped lang="less">
.hide {
  display: none;
}

.detail-content {
  display: flex;
  flex-direction: column;
  width: 1100px;
  margin: 4px auto;
}

.flex-view {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
}

.hidden-lg {
  display: none !important;
}

.thing-infos-view {
  display: flex;
  margin: 89px 0 40px;
  overflow: hidden;

  .thing-infos {
    -webkit-box-flex: 1;
    -ms-flex: 1;
    flex: 1;
    display: flex;
  }

  .mobile-share-box {
    height: 38px;
    background: transparent;
    padding: 0 16px;
    margin: 12px 0;
    font-size: 0;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    -webkit-box-pack: justify;
    -ms-flex-pack: justify;
    justify-content: space-between;

    .state {
      width: 64px;
      height: 24px;
      line-height: 24px;
      background: rgba(70, 132, 226, 0.1);
      border-radius: 2px;
      font-weight: 500;
      font-size: 12px;
      color: #4684e2;
      text-align: center;
    }

    .share-img {
      background: #fff;
      width: 38px;
      height: 38px;
      border-radius: 50%;
      text-align: center;

      img {
        position: relative;
        top: 4px;
        width: 24px;
      }
    }
  }

  .thing-img-box {
    -webkit-box-flex: 0;
    -ms-flex: 0 0 235px;
    flex: 0 0 235px;
    margin: 0 40px 0 0;

    img {
      width: 200px;
      height: 186px;
      display: block;
      background-size: cover;
      object-fit: cover;
    }
  }

  .thing-info-box {
    text-align: left;
    padding: 0;
    margin: 0;
  }

  .thing-state {
    height: 26px;
    line-height: 26px;

    .state {
      font-weight: 500;
      color: #4684e2;
      background: rgba(70, 132, 226, 0.1);
      border-radius: 2px;
      padding: 5px 8px;
      margin-right: 16px;
    }

    span {
      font-size: 14px;
      color: #152844;
    }
  }

  .thing-name {
    line-height: 32px;
    margin: 16px 0;
    color: #0f1111 !important;
    font-weight: 400 !important;
    font-style: normal !important;
    text-transform: none !important;
    text-decoration: none !important;
  }

  .translators,
  .authors {
    line-height: 18px;
    font-size: 14px;
    margin: 8px 0;
    -webkit-box-align: start;
    -ms-flex-align: start;
    align-items: flex-start;
    -webkit-box-pack: start;
    -ms-flex-pack: start;
    justify-content: flex-start;

    .name {
      color: #315c9e;
      white-space: normal;
    }
  }

  .tags {
    position: absolute;
    bottom: 20px;
    margin-top: 16px;

    .category-box {
      color: #152844;
      font-size: 14px;

      .title {
        color: #787878;
      }
    }
  }

  .thing-counts {
    -webkit-box-flex: 0;
    -ms-flex: 0 0 235px;
    flex: 0 0 235px;
    margin-left: 20px;
  }

  .pointer {
    cursor: pointer;
  }

  .count-item {
    height: 64px;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    cursor: pointer;
  }

  .count-img {
    -webkit-box-flex: 0;
    -ms-flex: 0 0 32px;
    flex: 0 0 32px;
    margin-right: 24px;
    font-size: 0;

    img {
      width: 100%;
      display: block;
    }
  }

  .count-box {
    position: relative;
    border-bottom: 1px solid #cedce4;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    -webkit-box-pack: justify;
    -ms-flex-pack: justify;
    justify-content: space-between;
    -webkit-box-flex: 1;
    -ms-flex: 1;
    flex: 1;
    height: 100%;
  }

  .count-text-box {
    font-size: 0;

    .count-title {
      color: #152844;
      font-weight: 600;
      font-size: 16px;
      line-height: 18px;
      display: block;
      height: 18px;
    }
  }

  .count-num-box {
    font-weight: 600;
    font-size: 20px;
    line-height: 24px;
    color: #152844;
  }
}

.buy-btn {
  cursor: pointer;
  display: block;
  background: #4684e2;
  border-radius: 4px;
  text-align: center;
  color: #fff;
  font-size: 14px;
  height: 36px;
  line-height: 36px;
  width: 110px;
  outline: none;
  border: none;
  margin-top: 18px;
}

.buy-btn img {
  width: 12px;
  margin-right: 2px;
  vertical-align: middle;
}

.buy-btn span {
  vertical-align: middle;
}

.buy-way {
  overflow: hidden;

  .title {
    font-weight: 600;
    font-size: 18px;
    height: 26px;
    line-height: 26px;
    color: #152844;
    margin-bottom: 12px;
  }
}

.thing-content-view {
  margin-top: 40px;
  padding-bottom: 50px;
}

.main-content {
  -webkit-box-flex: 1;
  -ms-flex: 1;
  flex: 1;

  .text {
    color: #484848;
    font-size: 16px;
    line-height: 26px;
    padding-left: 12px;
    margin: 11px 0;
    white-space: pre-wrap;
  }
}

.main-tab {
  border-bottom: 1px solid #cedce4;
}

.order-view {
  position: relative;
  color: #6c6c6c;
  font-size: 14px;
  line-height: 40px;

  .title {
    margin-right: 8px;
  }

  .tab {
    margin-right: 20px;
    cursor: pointer;
    color: #5f77a6;
    font-size: 16px;
    cursor: pointer;
  }

  .tab-select {
    color: #152844;
    font-weight: 600;
  }

  .tab-underline {
    position: absolute;
    bottom: 0;
    left: 84px;
    width: 16px;
    height: 4px;
    background: #4684e2;
    -webkit-transition: left 0.3s;
    transition: left 0.3s;
  }
}

.recommend {
  -webkit-box-flex: 0;
  -ms-flex: 0 0 235px;
  flex: 0 0 235px;
  margin-left: 20px;

  .title {
    font-weight: 600;
    font-size: 18px;
    line-height: 26px;
    color: #152844;
    margin-bottom: 12px;
  }

  .things {
    border-top: 1px solid #cedce4;

    .thing-item {
      min-width: 255px;
      max-width: 255px;
      position: relative;
      flex: 1;
      margin-right: 20px;
      height: fit-content;
      overflow: hidden;
      margin-top: 26px;
      margin-bottom: 36px;
      padding-bottom: 24px;
      border-bottom: 1px #e1e1e1 solid;
      cursor: pointer;

      .img-view {
        //background: #f3f3f3;
        //text-align: center;
        height: 200px;
        width: 255px;
        //border: 1px #f3f3f3 solid;

        img {
          height: 200px;
          width: 240px;
          overflow: hidden;
          margin: 0 auto;
          background-size: cover;
          object-fit: cover;
        }
      }

      .info-view {
        //background: #f6f9fb;
        overflow: hidden;
        padding: 0 16px;

        .thing-name {
          line-height: 32px;
          margin-top: 12px;
          color: #0f1111 !important;
          font-weight: 400 !important;
          font-style: normal !important;
          text-transform: none !important;
          text-decoration: none !important;
        }

        .price {
          color: #ff7b31;
          font-size: 20px;
          line-height: 20px;
          margin-top: 4px;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }

        .translators {
          color: #6f6f6f;
          font-size: 12px;
          line-height: 14px;
          margin-top: 4px;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
      }
    }
  }
}

.flex-view {
  display: flex;
}

.thing-comment {
  .title {
    font-weight: 600;
    font-size: 14px;
    line-height: 22px;
    height: 22px;
    color: #152844;
    margin: 24px 0 12px;
  }

  .publish {
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;

    .mine-img {
      -webkit-box-flex: 0;
      -ms-flex: 0 0 40px;
      flex: 0 0 40px;
      margin-right: 12px;
      border-radius: 50%;
      width: 40px;
      height: 40px;
    }

    .content-input {
      -webkit-box-flex: 1;
      -ms-flex: 1;
      flex: 1;
      background: #f6f9fb;
      border-radius: 4px;
      height: 32px;
      line-height: 32px;
      color: #484848;
      padding: 5px 12px;
      white-space: nowrap;
      outline: none;
      border: 0px;
    }

    .send-btn {
      margin-left: 10px;
      background: #4684e2;
      border-radius: 4px;
      -webkit-box-flex: 0;
      -ms-flex: 0 0 80px;
      flex: 0 0 80px;
      color: #fff;
      font-size: 14px;
      text-align: center;
      height: 32px;
      line-height: 32px;
      outline: none;
      border: 0px;
      cursor: pointer;
    }
  }

  .tab-view {
    -webkit-box-pack: justify;
    -ms-flex-pack: justify;
    justify-content: space-between;
    font-size: 14px;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    margin: 24px 0;

    .count-text {
      color: #484848;
      float: left;
    }

    .tab-box {
      color: #5f77a6;
      -webkit-box-align: center;
      -ms-flex-align: center;
      align-items: center;

      .tab-select {
        color: #152844;
      }

      span {
        cursor: pointer;
      }
    }

    .line {
      width: 1px;
      height: 12px;
      margin: 0 12px;
      background: #cedce4;
    }
  }
}

.comments-list {
  .comment-item {
    .flex-item {
      -webkit-box-align: center;
      -ms-flex-align: center;
      align-items: center;
      padding-top: 16px;

      .avator {
        -webkit-box-flex: 0;
        -ms-flex: 0 0 40px;
        flex: 0 0 40px;
        width: 40px;
        height: 40px;
        margin-right: 12px;
        border-radius: 50%;
        cursor: pointer;
      }

      .person {
        -webkit-box-flex: 1;
        -ms-flex: 1;
        flex: 1;
      }

      .name {
        color: #152844;
        font-weight: 600;
        font-size: 14px;
        line-height: 22px;
        height: 22px;
        cursor: pointer;
      }

      .time {
        color: #5f77a6;
        font-size: 12px;
        line-height: 16px;
        height: 16px;
        margin-top: 2px;
      }

      .float-right {
        color: #4684e2;
        font-size: 14px;
        float: right;

        span {
          margin-left: 19px;
          cursor: pointer;
        }

        .num {
          color: #152844;
          margin-left: 6px;
          cursor: auto;
        }
      }
    }
  }
}

.comment-content {
  margin-top: 8px;
  color: #484848;
  font-size: 14px;
  line-height: 22px;
  padding-bottom: 16px;
  border-bottom: 1px dashed #cedce4;
  margin-left: 52px;
  overflow: hidden;
  word-break: break-word;
}

.infinite-loading-container {
  clear: both;
  text-align: center;
}

.a-price-symbol {
  top: -0.5em;
  font-size: 12px;
}

.a-price {
  color: #0f1111;
  font-size: 12px;
}
.a-price-1{
  color: #0f1111;
  font-size: 4px;
}
</style>
